package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.DesUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.MyCountDown;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity implements HttpCallback {

    @BindView(R.id.reg_mobile)
    EditText regMobile;
    @BindView(R.id.reg_threeCode)
    EditText regThreeCode;
    @BindView(R.id.reg_threeGetCode)
    TextView regThreeGetCode;
    @BindView(R.id.reg_threePassword)
    EditText regThreePassword;
    @BindView(R.id.people_layout)
    AutoRelativeLayout peopleLayout;
    @BindView(R.id.reg_threeBtn)
    Button regThreeBtn;

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("找回密码");
        peopleLayout.setVisibility(View.GONE);
        regThreeBtn.setText("确定找回");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {

    }

    @OnClick({R.id.reg_threeGetCode, R.id.reg_threeBtn})
    public void onClick(View view) {
        String mobile = regMobile.getText().toString();
        String code = regThreeCode.getText().toString();
        String password = regThreePassword.getText().toString();
        switch (view.getId()) {
            case R.id.reg_threeGetCode:
                if (!BaseUtils.isMobileNO(mobile)) {
                    showToast("请填写正确的手机号码");
                    return;
                } else {
                    HttpUtils.getInstance().postKey(
                            GetJsonUtils.getInstance().getRequest(HttpURL.SMS_SEND, mContext,
                                    new String[]{"accessid", "user_mobile", "sms_type"},
                                    new String[]{CommonValue.accessid_value, mobile, "1"}),
                            mContext , CommonValue.REQUEST_CODE1, this);
                }
                break;
            case R.id.reg_threeBtn:
                if (!BaseUtils.isMobileNO(mobile)) {
                    showToast("请填写正确的手机号码");
                    return;
                } else if (TextUtils.isEmpty(code)) {
                    showToast("请填写验证码");
                    return;
                } else if (TextUtils.isEmpty(password)) {
                    showToast("请填写密码");
                    return;
                } else {
                    HttpUtils.getInstance().postKey(GetJsonUtils.getInstance().getRequest(HttpURL.USER_IFORGOT, mContext,
                            new String[]{"accessid", "user_mobile", "valicode", "user_password"},
                            new String[]{CommonValue.accessid_value, mobile, code, DesUtils.encrypt(password, CommonValue.Des_value)}),
                            mContext , CommonValue.REQUEST_CODE2, this);
                }
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                showToast("验证码发送成功");
                new MyCountDown(60 * 1000, 1000, regThreeGetCode).start();
                break;
            case CommonValue.REQUEST_CODE2:
                showToast("修改密码成功");
                finish();
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
